/*
 ScoreKeeper.cpp
 */

#include <iostream>
#include <string>

using namespace std;

#include "player.h"
#include "scoreKeeper.h"


ScoreKeeper::ScoreKeeper()
{
	used = 0;
	// the players array will automatically 
	// be initialized using the default 
	// constructor of the Player class
}

Player ScoreKeeper::getPlayer(int playerIndex)
{
	Player tmpPlayer;
	if(playerIndex >= 0 && playerIndex < MAX)
		tmpPlayer = players[playerIndex];
	return(tmpPlayer);
}

int ScoreKeeper::getUsed()
{
	return(used);
}


void ScoreKeeper::addPlayer(Player playerToAdd)
{
	if(used < MAX)
	{
		players[used] = playerToAdd;
		used++;
	}
}


void ScoreKeeper::IncrementPlayerScore(string playerName)
{
	int i = -1;
	for(i = 0; i < used; i++)
		if(players[i].getName() == playerName)
			break;

	if(i >= 0 && i < used)
		players[i].setScore( players[i].getScore()+1 );
	else
		cout << "\nIncorrect playerName\n";
}

void ScoreKeeper::removePlayer(Player playerToRemove)
{
	int indexToRemove = findPlayer(playerToRemove);
	if(indexToRemove >= 0 && indexToRemove < used)
	{
		used--;
		players[indexToRemove] = players[used];
	}
}

int ScoreKeeper::findPlayer(Player playerToFind)
{
	int indexFound = -1;
	int i = 0;
	for(i = 0; i < used; i++)
		if(players[i].getName() == playerToFind.getName() &&
		   players[i].getScore() == playerToFind.getScore())
			break;
	return(indexFound);
}


void ScoreKeeper::printScoreKeeperInfo()
{
	cout << "\n\n******************* SCORE KEEPER ********************\n\n";
	for(int i = 0; i < used; i++)
		cout << players[i].FormatPlayerInfo() << endl;
	cout << endl;
}

